//
//  HMWelcomeViewController.swift
//  Weibo
//
//  Created by 李学林 on 15/12/9.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
import SDWebImage


class HMWelcomeViewController: UIViewController {

    override func loadView() {
        super.loadView()
        view = bgImageView
        setupUI()
    }

    private func setupUI(){
        // 往view上添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        // 设置控件的约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(20)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 更新约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        messageLabel.alpha = 0
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
         
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1;
                    }, completion: { (_) -> Void in
                        // 切换到主控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootVCNotifation, object: nil)
                        
                })
        }
        
    }

    // 懒加载背景imageView
    lazy var bgImageView : UIImageView = {
        let image = UIImage(named: "ad_background")
        let imageView = UIImageView(image: image)
        imageView.cornerRadius = 25
        return imageView
    }()
    // 头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
//        imageView.sd_setImageWithURL(NSURL(string: (HMWBUserInfoViewModel.sharedAccount.userAccount?.avatar_large!)!), placeholderImage: UIImage(named: "avatar_default_big"));
        printLog(HMWBUserInfoViewModel.sharedAccount.userAccount!.avatar_large)
        imageView.sd_setImageWithURL(NSURL(string: HMWBUserInfoViewModel.sharedAccount.userAccount!.avatar_large!)!, placeholderImage: UIImage(named: "avatar_default_big"))

        imageView.cornerRadius = 45
        return imageView
    }()
    // 欢迎回来label
    lazy var messageLabel : UILabel = {
        let messageLabel = UILabel()
        messageLabel.text = "欢迎回来"
        messageLabel.font = UIFont.systemFontOfSize(14)
        messageLabel.textColor = UIColor.darkGrayColor()
        return messageLabel
    }()
    
    
}
